﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ivan_snake
{
    class Snake: IEnumerable<SnakePart>
    {
        public  List<SnakePart> snake;
        private bool _life;
        private bool _controlLife;
        public SnakePart  snakeHead   ;

        public SnakePart SnakeHead
        {
           
            get {return snake.First();}
         }

         public Snake()
        {

            
            snake = new List<SnakePart>(0);
            for (int i = 0; i < 7; i++)
            {

                snake.Add(new SnakePart(20 + i, 20));
            }
         }
       
        public void AddElement(SnakePart item)
        {
            snake.Insert(0, item);

        }
       
        public void MoveSnakeRight()
        {
            snake.Insert(snake.Count, new SnakePart(snake[snake.Count - 1].X + 1, snake[snake.Count - 1].Y));
            snake.RemoveAt(0);
        }
        public void MoveSnakeLeft()
        {
            snake.Insert(snake.Count, new SnakePart(snake[snake.Count - 1].X - 1, snake[snake.Count - 1].Y));
            snake.RemoveAt(0);
        }
        public void MoveSnakeUp()
        {
            snake.Insert(snake.Count, new SnakePart(snake[snake.Count - 1].X, snake[snake.Count - 1].Y - 1));
            snake.RemoveAt(0);
        }
        public void MoveSnakeDown()
        {
            snake.Insert(snake.Count, new SnakePart(snake[snake.Count - 1].X, snake[snake.Count - 1].Y + 1));
            snake.RemoveAt(0);
        }
   

    public   Food  NextItem()
        {
            Random rnd1 = new Random();
            Random rnd2 = new Random();
            int rW = rnd1.Next(2, Console.WindowWidth - 2);
            int rH = rnd2.Next(2, Console.WindowHeight - 2);
            bool error;
            do
            {
                error = false;
                foreach (var snakeBody in snake)
                {
                    if (snakeBody.X == rW && snakeBody.Y == rH)
                    {
                        error = true;
                        break;
                    }
                }
                if (error)
                {
                    rW = rnd1.Next(2, Console.WindowWidth - 2);
                    rH = rnd2.Next(2, Console.WindowHeight - 2);
                }

            } while (error);

            //return new Food(rW, rH);
            return new Food(10, 15);
        }


        public IEnumerator<SnakePart> GetEnumerator()
        {
            return this.snake.GetEnumerator();
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return this.snake.GetEnumerator();
        }
    }
}
